package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.PublishCourseVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-03
 */
@RestController
@RequestMapping("/eduservice/course")
@Api(tags = "课程管理")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation("添加课程信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = eduCourseService.addCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }
    @ApiOperation("根据id查询课程信息")
    @GetMapping("getCourseInfo/{id}")
    public R getCourseInfo(@PathVariable  String id) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseById(id);
        return R.ok().data("courseInfo",courseInfoVo);
    }
    @ApiOperation("根据id修改课程信息")
    @PutMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody  CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseInfoVo.getId());
    }
    @ApiOperation("获取发布的课程信息")
    @GetMapping("getPublishCourse/{id}")
    public R getPublishCourse(@PathVariable String id) {
        PublishCourseVo publishCourseVo = eduCourseService.getPublishCourse(id);
        return R.ok().data("item",publishCourseVo);
    }

    @ApiOperation("最终发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean b = eduCourseService.updateById(eduCourse);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("查询课程列表")
    @GetMapping("queryCourseList")
    public R queryCourseList(@RequestParam Long pageNum,@RequestParam Long pageSize,@RequestParam(required = false) String title) {
        Page<EduCourse> coursePage =  eduCourseService.queryCourse(pageNum,pageSize,title);
        List<EduCourse> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        return R.ok().data("records",records).data("total",total);
    }
    @ApiOperation("删除课程")
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.deleteCourseById(courseId);
        return R.ok();
    }


}

